﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ZXW.DotNet.Common.Basic.Network.Tcp
{
    /// <summary>
    /// TCP连接对。
    /// </summary>
    public class ConnectionPair : IDisposable
    {
        #region Ctor
        public ConnectionPair(NetworkStream client, NetworkStream proxy, IPEndPoint client_ipe, int proxyBufferSize, int clientBufferSize, int _mappingPort)
        {
            this.clientStream = client;
            this.proxyStream = proxy;
            this.clientIPE = client_ipe;
            this.proxyBuffer = new byte[proxyBufferSize];
            this.clientBuffer = new byte[clientBufferSize];
            this.mappingPort = _mappingPort;
        }
        #endregion

        #region CreateTime
        private DateTime createTime = DateTime.Now;
        /// <summary>
        /// 连接对的创建时间。
        /// </summary>
        public DateTime CreateTime
        {
            get { return createTime; }
        }
        #endregion

        #region LastDataFromClientTime
        private DateTime lastDataFromClientTime = DateTime.Now;
        /// <summary>
        /// 从客户端接收到的最后一条消息的时间。
        /// </summary>
        public DateTime LastDataFromClientTime
        {
            get { return lastDataFromClientTime; }
        }
        #endregion

        #region LastDataFromServerTime
        private DateTime lastDataFromServerTime = DateTime.Now;
        /// <summary>
        /// 从服务器接收到的最后一条消息的时间。
        /// </summary>
        public DateTime LastDataFromServerTime
        {
            get { return lastDataFromServerTime; }
        }
        #endregion

        #region BytesFromServer
        private uint bytesFromServer = 0;
        /// <summary>
        /// 转发的服务器发送给客户端的字节数。
        /// </summary>
        public uint BytesFromServer
        {
            get { return bytesFromServer; }
            set
            {
                bytesFromServer = value;
                this.lastDataFromServerTime = DateTime.Now;
            }
        }
        #endregion

        #region BytesFromClient
        private uint bytesFromClient = 0;
        /// <summary>
        /// 转发的客户端服发送给务器的字节数。
        /// </summary>
        public uint BytesFromClient
        {
            get { return bytesFromClient; }
            set
            {
                bytesFromClient = value;
                this.lastDataFromClientTime = DateTime.Now;
            }
        }
        #endregion

        #region ClientBuffer
        private byte[] clientBuffer;
        /// <summary>
        /// 用于转发客户端发送给真实服务器的消息的缓冲区
        /// </summary>
        public byte[] ClientBuffer
        {
            get { return clientBuffer; }
        }
        #endregion

        #region ProxyBuffer
        private byte[] proxyBuffer;
        /// <summary>
        /// 用于转发真实服务器发送给客户端的消息的缓冲区
        /// </summary>
        public byte[] ProxyBuffer
        {
            get { return proxyBuffer; }
        }
        #endregion

        #region ClientStream
        private NetworkStream clientStream;
        /// <summary>
        /// 客户端与代理服务器的连接。
        /// </summary>
        public NetworkStream ClientStream
        {
            get { return clientStream; }
        }
        #endregion

        #region ProxyStream
        private NetworkStream proxyStream;
        /// <summary>
        /// 代理服务器与真实服务器的连接。
        /// </summary>
        public NetworkStream ProxyStream
        {
            get { return proxyStream; }
        }
        #endregion

        #region MappingPort
        private int mappingPort = 0;
        /// <summary>
        /// 当前ProxyStream与服务器通信的端口。
        /// </summary>
        public int MappingPort
        {
            get { return mappingPort; }
        }
        #endregion

        #region ClientIPE
        private IPEndPoint clientIPE;
        /// <summary>
        /// 客户端的IPE
        /// </summary>
        public IPEndPoint ClientIPE
        {
            get { return clientIPE; }
        }
        #endregion

        #region Dispose
        public void Dispose()
        {
            try
            {
                this.clientStream.Close();
                this.clientStream.Dispose();
                this.proxyStream.Close();
                this.proxyStream.Dispose();
            }
            catch { }
        }
        #endregion
    }
}
